package us.mmapp.util;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBOpenHelper extends SQLiteOpenHelper
{
	public final static String ID="ID";
	public final static String UID="UID";
	public final static String TYPE="TYPE";
	public final static String DATE_CREATED="DATE_CREATED";
	public final static String LATITUDE="LATITUDE";
	public final static String LONGITUDE="LONGITUDE";
	public final static String STOREAGE_LOCATION="STOREAGE_LOCATION";
	public final static String TITLE="TITLE";
	public final static String DESCRIPTION="DESCRIPTION";
	public final static String PRIVACY="PRIVACY";
	public final static String DATE_MODIFIED="DATE_MODIFIED";
	public final static String SID="SERVERID";
	public final static String URL="URL";
	public final static String URL_THUMB="URL_THUMB";
	public final static String SYNCED="SYNCED";
	public final static String LOCAL_THUMB="LOCAL_THUMB";
	
	public final static int id=0;
	public final static int uid=2;
	public final static int type=4;
	public final static int date_created=8;
	public final static int date_modified=9;
	public final static int latitude=10;
	public final static int longitude=11;
	public final static int storeage_location=3;
	public final static int title=5;
	public final static int description=6;
	public final static int privacy=7;
	public final static int sid=1;
	public final static int url=12;
	public final static int url_thumb=13;
	public final static int synced=14;
	public final static int local_thumb=15;
	
	
	public static int VERSION=5;
	public DBOpenHelper(Context context, String dbName, int version)
	{
		super(context, dbName, null, version);
	}

	/*
	 * Called when the database has been opened. The implementation should check
	 * isReadOnly() before updating the database.
	 */
	@Override
	public void onCreate(SQLiteDatabase db)
	{
		try
		{
			db.execSQL("CREATE TABLE IF NOT EXISTS OBJECTS(ID TEXT PRIMARY KEY, SERVERID INTEGER, UID INTEGER, STOREAGE_LOCATION TEXT, TYPE INTEGER, TITLE TEXT, DESCRIPTION TEXT, PRIVACY INTEGER, DATE_CREATED DATE, DATE_MODIFIED DATE, LATITUDE DOUBLE, LONGITUDE DOUBLE, URL TEXT, URL_THUMB TEXT, SYNCED INTEGER, LOCAL_THUMB TEXT)");
			db.execSQL("CREATE TABLE IF NOT EXISTS TAGS(ID TEXT PRIMARY KEY, TAG TEXT)");
			db.execSQL("CREATE TABLE IF NOT EXISTS COMMENTS(OID INTEGER, UID INTEGER, TIME DATE, COMMENT TEXT, PRIMARY KEY(COMMENT, OID))");
			db.execSQL("CREATE TABLE IF NOT EXISTS EXTRA_FIELDS(FIELD TEXT, INFO TEXT, ORDER_ID INTEGER, OID INTEGER, PRIMARY KEY(FIELD, OID))");
			//db.close();
			
			//db.execSQL("CREATE TABLE IF NOT EXISTS Objects (ID STRING PRIMARY KEY, URL TEXT)");
		}
		catch (SQLException e)
		{
			Log.e("SqliteAndroid", "DBOpenHelper", e);
		}
	}

	/*
	 * Called when the database needs to be upgraded. The implementation should
	 * use this method to drop tables, add tables, or do anything else it needs
	 * to upgrade to the new schema version.
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		db.execSQL("DROP TABLE IF EXISTS objects");
		db.execSQL("DROP TABLE IF EXISTS tags");
		db.execSQL("DROP TABLE IF EXISTS comments");
		db.execSQL("DROP TABLE IF EXISTS extra_fields");
		this.onCreate(db);
	}
}
